<?php

class C_user extends CI_Controller {

	
	function __construct(){
		parent::__construct();
		session_start(); 
		$this->load->model('m_user');
		
		$this->load->library('form_validation');
		$this->load->helper('form');
		$this->load->helper('url');
		$this->form_validation->set_message('valid_email', '%s harus diisi dengan alamat email');
		$this->form_validation->set_message('required', '%s tidak boleh kosong, harus diisi');
		$this->form_validation->set_message('numeric', 'isi dengan angka');
		$this->form_validation->set_message('max_length', '%s tidak boleh melebihi %s huruf');
		$this->form_validation->set_message('is_natural_no_zero', 'harus diisi dengan angka');
	}
	
	function index(){
		$data['content_page'] = $this->load->view('v_user',null,TRUE);
		$data['title_page'] = "User";
		$this->load->view('v_main',$data);
	}
	
	function get_username(){
		$userid=$this->input->post('username');
		$result=$this->m_user->get_username($userid);
		echo $result;
	}
	

	function cek_serial(){
		$serial=$this->input->post('serial');
		$result=$this->m_user->cek_serial($serial);
		echo $result;
	}
	
	function cek_profile(){
		$result=$this->m_user->cek_profile();
		echo $result;
	}
	
	function save(){
		
		$username=$this->input->post('username');
		$nama=$this->input->post('nama');
		$password=md5($this->input->post('password'));
		$serial=$this->input->post('serial');
		$newsletter=$this->input->post('newsletter');
		$keycode=$this->input->post('keycode');
		$ipAddr=$_SERVER['REMOTE_ADDR'];
		
		$result=$this->m_public_function->cek_keycode($keycode, $ipAddr);
		if($result){
			$result=$this->m_user->save($username, $nama, $password, $serial, $newsletter );
			echo $result;
		}else{
			echo -1;
		}
	}
}
